package com.perye.dokit.weController;


import cn.hutool.core.util.IdUtil;
import com.perye.dokit.aop.log.Log;
import com.perye.dokit.base.BaseController;
import com.perye.dokit.service.WeCompInfoService;
import com.perye.dokit.utils.DateUtils;
import com.perye.dokit.utils.StringUtils;
import com.perye.dokit.utils.pageData.PageData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.method.P;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "基础信息管理")
@RestController
@CrossOrigin
@RequestMapping("/api/WeCompInfoController")
public class WeCompInfoController extends BaseController {


    @Autowired
    private WeCompInfoService weCompInfoService;


    @Log("查询基础信息列表")
    @ApiOperation("查询基础信息列表")
    @GetMapping("/getall")
    public List<PageData> getall() throws Exception{
        List<PageData> list=weCompInfoService.getall();
        return list;
    }

    @PostMapping("/getInfoName")
    public PageData getInfoName(PageData pageData) throws Exception {
     PageData pageData1= getPageData();
     PageData pageData2=weCompInfoService.selInfoName(pageData);

     return pageData2;
    }

//    @GetMapping("/Sel/{id}")
//    public PageData getInfoId(@PathVariable("id") PageData pageData) throws Exception {
//        PageData pageData1=getPageData();
//        PageData  pageData2=weCompInfoService.selInfoId(pageData1);
//        return pageData2;
//    }

    @PostMapping("/Sel")
    public PageData getInfoId(@RequestBody PageData pageData) throws Exception {
        PageData pageData1= weCompInfoService.selInfoId(pageData);
       String s=pageData1.get("url").toString();

       String[] as=s.split(",");
       List<PageData> picList = new ArrayList<PageData>();
       List<String> imageList = new ArrayList<String>();
          List<PageData>  violist=new ArrayList<PageData>();
        List<String>  violist1=new ArrayList<String>();
        PageData pageData2=new PageData();
       for (int i=0;i<as.length;i++){
           if (StringUtils.isNotBlank(as[i])){
               PageData pic = new PageData();

           if (as[i].contains(".mp4")){

               pageData2.put("id",i+"");
               pageData2.put("vurl",as[i]);
               violist.add(pageData2);
               violist1.add(as[i]);
//               System.out.println(as[i]+"fwef5g");
               continue;
           }
           //将循环读取出来的图片添加到list集合中
               System.out.println(as[i]);
               pic.put("name",i+"");
               pic.put("url",as[i]);
               picList.add(pic);
               imageList.add(as[i]);
           }
       }
       pageData1.put("code",00);
       pageData1.put("imageList",imageList);
       pageData1.put("url",picList);
       pageData1.put("vrl",violist);
        pageData1.put("violist1",violist1);
//      pageData1.put("data",pageData1);
      pageData1.put("msg","查询成功");
        return pageData1;
    }

    @PostMapping("/addInfo")
    public PageData addinfo(@RequestBody PageData pageData) throws Exception {

             PageData pageData1=new PageData();
             String id= IdUtil.simpleUUID();
             List<PageData> url=(List<PageData>) pageData.get("picList");
//        System.out.println(url+"sfsdfsafwefwfz");
//            String videourl=pageData.get("videourl").toString();
        String videourl=pageData.get("videourl").toString();
             pageData1.put("id",id);
             pageData1.put("create_time",DateUtils.getTime());
             pageData1.put("update_time",DateUtils.getTime());
             pageData1.put("NAME","6");
             pageData1.put("address",pageData.get("address"));
             pageData1.put("brief_introduction","6");
             pageData1.put("contacts",pageData.get("contacts"));
             pageData1.put("wechat",pageData.get("wechat"));
             pageData1.put("phone",pageData.get("phone"));
             Integer result=weCompInfoService.addinfo(pageData1);   //新增基础信息

            PageData pd=new PageData();
        for (int i=0;i<url.size();i++){    //新增图片
            pd.put("id",IdUtil.simpleUUID());
            pd.put("relation_id",id);
            pd.put("type",0);
            pd.put("url",url.get(i));
            pd.put("sort",0);
            pd.put("is_delete",0);
            pd.put("create_time", DateUtils.getTime());
            pd.put("update_time", DateUtils.getTime());
            weCompInfoService.addimg(pd);
        }
        PageData pe=new PageData();
        pe.put("id",IdUtil.simpleUUID());
        pe.put("relation_id",id);
        pe.put("type",1);
        pe.put("url",videourl);
        pe.put("sort",0);
        pe.put("is_delete",0);
        pe.put("create_time", DateUtils.getTime());
        pe.put("update_time", DateUtils.getTime());
        weCompInfoService.addimg(pe);                           //新增视频
            PageData result1=new PageData();
            result1.put("code",00);
            result1.put("data",pageData1);
            result1.put("msg","新增成功");

//        Integer result=weCompInfoService.addinfo(pageData);
        return result1;
    }

    @PutMapping("/updInfo")
    public Integer updinfo(@RequestBody  PageData pageData) throws Exception {

        System.out.println("进入此方法");    //修改基础信息
        Integer result=weCompInfoService.updinfo(pageData);

        PageData pageData1=new PageData();  //删除图片
        pageData1.put("relation_id",pageData.getString("id").toString());
        Integer re=weCompInfoService.delimg(pageData1);

        List<PageData> list= (List<PageData>) pageData.get("imageList");
//        String vurl=pageData.get("videourl").toString();
         String vurl=pageData.get("violist").toString();
//        System.out.println(vurl);
//         List<PageData> list1= (List<PageData>) pageData.get("violist");


//        System.out.println(vurl+"23rfdsfwe");
//        System.out.println(pageData.get("imageList"));

          PageData pd=new PageData();    //新增图片
          for (int i=0;i<list.size();i++){
              pd.put("id",IdUtil.simpleUUID());
              pd.put("relation_id",pageData.get("id").toString());
              pd.put("type",0);
              pd.put("url",list.get(i));
              pd.put("sort",0);
              pd.put("is_delete",0);
              pd.put("create_time", DateUtils.getTime());
              pd.put("update_time", DateUtils.getTime());
              weCompInfoService.addimg(pd);
          }
        PageData pe=new PageData();
//   for (int i=0;i<list1.size();i++){
       pe.put("id",IdUtil.simpleUUID());
       pe.put("relation_id",pageData.get("id").toString());
       pe.put("type",1);
       pe.put("url",vurl);
       pe.put("sort",0);
       pe.put("is_delete",0);
       pe.put("create_time", DateUtils.getTime());
       pe.put("update_time", DateUtils.getTime());
       weCompInfoService.addimg(pe);
//   }
        return result;
    }
    //删除
    @PostMapping("/del")
    public Integer del(@RequestBody PageData pageData) throws Exception {
    int result=weCompInfoService.delinfo(pageData);
    return result;
    }



    @Value("${filepathinfo}")
    public String filepathinfo;     //文件地址

       @RequestMapping("/eluploadvid")
       public PageData elUpload(MultipartFile file){
    //单文件上传
        String fileName = file.getOriginalFilename();
        PageData result=new PageData();

        File dest = new File(filepathinfo + fileName);    //文件全路径
        String filepath=dest.toString();


//        int index=filepath.lastIndexOf(".");                 //根据下标获取文件后缀
//        String hz=filepath.substring(index+1,filepath.length());   //根据下标获取文件后缀

        try {
            file.transferTo(dest);
            result.put("code",00);
            result.put("filepathinfo",filepath);
            result.put("fileid",0);
            result.put("msg","上传成功");
        } catch (IOException e) {
            e.printStackTrace();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return  result;
    }


        @RequestMapping("/elupload")
        public PageData fileUpload(@RequestParam("files")MultipartFile[] files) throws Exception {
            String filePath = "";
            List<String> list=new ArrayList<>();
            // 多文件上传
            PageData result=new PageData();
            for (MultipartFile file : files){
                // 上传简单文件名
                String originalFilename = file.getOriginalFilename();
                // 存储路径
                filePath = new StringBuilder(filepathinfo)
                        .append(originalFilename)
                        .toString();
//                list.add(filePath);
                try {
                    // 保存文件
                    file.transferTo(new File(filePath));
//                    System.out.println("测试测试测试测试测试测试");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(filePath);
            result.put("code",00);
            result.put("imgurl",filePath);
            result.put("msg","成功");
            return result;
        }
    }


